//
// Created by Administrator on 2020/7/9.
//T5章:数组、指针与字符串
//综合实例:
//个人银行账户管理程序
//1.一个活期存储账户包括:
//           信息:账号(id)、余额(balance)、年利率(rate)等
//           操作:显示账户信息(show)、存款(deposit)、取款(withdraw)、结算利息(settle)等

#include <iostream>
#include <cmath>
using namespace std;

class SavingAccount//存储账户类
{
private:
    int id;//账户
    double balance;//余额
    double rate;//存款的年利率
    int lastDate;//上次变更余额的时期
    double accumulation;//余额按日累加之和

    //记录一笔账,date为日期,amount为金额,desc为说明
    void record(int date,double amount);
    //获得到指定日期为止的存款金额按日累积值
    double accumulate(int date)const
    {
        return accumulation+balance*(date-lastDate);
    }

public:
    //构造函数
    SavingAccount(int date,int id,double rate);
    int getId(){return id;}
    double getBalance(){return balance;}
    double getRate(){return rate;}

    //存入现金
    void deposit(int date,double amount);
    //取出现金
    void withdraw(int date,double amount);
    //结算利息,每年1月1日调用一次该函数
    void settle(int date);
    //显示账户信息
    void show();
};

//SavingAccount类相关成员函数的实现
SavingAccount::SavingAccount(int date, int id, double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
{
    cout<<date<<"\t#"<<id<<"is created"<<endl;
}

void SavingAccount::record(int date, double amount)
{
    accumulation=accumulate(date);
    lastDate=date;
    amount=floor(amount*100+0.5)/100;//保留小数点后两位(floor高斯函数)
    balance+=amount;
    cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}

void SavingAccount::settle(int date)
{
    double interest=accumulate(date)*rate/365;//计算年息
    if(interest!=0)
        record(date,interest);
    accumulation=0;
}

void SavingAccount::show() {cout<<'#'<<id<<"\tBalance:"<<balance;}
int main()
{
    //建立几个账户
    SavingAccount sa0(1,21325323,0.015);
    SavingAccount sa1(1,57453556,0.015);
    //几笔账目
    sa0.deposit(5,5000);
    sa1.deposit(25,10000);
    sa0.deposit(45,5500);
    sa1.deposit(60,4000);
    //开户后第90天到了银行的计息日,结算所有账户的年息
    sa0.settle(90);
    sa1.settle(90);
    //输出各个账户信息
    sa0.show();cout<<endl;
    sa1.show();cout<<endl;
    return 0;
}